from myelin.server.rules.Job import Generator
from myelin.server.core.ft import ModelDriver

from nodetypes.models.Container import Container
from nodetypes.models.rules.Rule import Rule

class Generator(Generator):
    
    dataModel = Container
    ruleModel = Rule
    
    newDbDriver = staticmethod(ModelDriver.FromRepo)
     
    def next(self, repo, server):
        mget = server.models.get
        
        rulesDriver = self.ruleModel.Initialize(
            self.newDbDriver(
                repo,
                mget('rules')
            )
        )
        # we need multiple model driver to
        # fetch for 'published.content', 'unpublished.content', 'archived.content'
        model = self.dataModel.Retrieve(
            self.dataModel.Initialize(
                self.newDbDriver(
                    repo,
                    mget('published.content')
                ),
            ),
            self.nodeIds.pop(0)
        )
        
        self.nodeIds.extend(
            m.rdf_value for m in model.mHasPart \
            if m.rdf_reset(rulesDriver).rRule_For
        )
        return [
            m for m in model.rdf_reset(rulesDriver).rRule_For \
            if m.rdf_reset(rulesDriver).rActive
        ]